import pygame
from pygame.sprite import Sprite


class Enemy(Sprite):
    """表示单个敌人的类"""

    def __init__(self, settings, screen):
        """初始化敌人并设置其起始位置"""
        super().__init__()
        self.screen = screen
        self.settings = settings

        # 加载敌人图像，并设置其rect属性
        self.image = pygame.image.load('images_source/enemy.png')
        self.rect = self.image.get_rect()

        # 每个敌人最初都在屏幕左上角附近（左边距，上边距）
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # 存储敌人的准确位置
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

    def draw_enemy(self):
        """在指定位置绘制敌人"""
        self.screen.blit(self.image, self.rect)

    def boundary_check(self):
        """如果敌人舰队位于边缘，则返回true"""
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right \
                or self.rect.left <= 0:
            return True

    def update(self):
        """向左或向右移动敌人"""
        self.x += (self.settings.enemy_speed_factor *
                   self.settings.fleet_direction)
        self.y += self.settings.enemy_drop_speed_factor
        self.rect.x = self.x
        self.rect.y = self.y
